package com.egao.common.module.world.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.world.entity.WorldGameArticle;
import com.egao.common.module.world.mapper.WorldGameArticleMapper;
import com.egao.common.module.world.service.WorldGameArticleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文章管理实现类
 *
 * @author pyx
 */
@Service
public class WorldGameArticleServiceImpl extends ServiceImpl<WorldGameArticleMapper, WorldGameArticle> implements WorldGameArticleService {

    @Override
    public List<WorldGameArticle> listPage(PageParam<WorldGameArticle> pageParam) {
        return baseMapper.listPage(pageParam);
    }

    @Override
    public JsonResult insertArticle(WorldGameArticle worldGameArticle) {
        if (worldGameArticle == null) {
            throw new ParameterException("参数对象为空");
        }
        if (StrUtil.isBlank(worldGameArticle.getTitle())) {
            throw new ParameterException("标题不能为空");
        }
        if (baseMapper.insert(worldGameArticle) > 0) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Override
    public JsonResult updateArticle(WorldGameArticle worldGameArticle) {
        if (worldGameArticle == null) {
            throw new ParameterException("参数对象为空");
        }
        if (StrUtil.isBlank(worldGameArticle.getTitle())) {
            throw new ParameterException("标题不能为空");
        }
        if (baseMapper.updateById(worldGameArticle) > 0) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult deleteArticle(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }
        if (removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult getId(Integer id) {
        WorldGameArticle worldGameArticle = baseMapper.selectById(id);
        if (worldGameArticle == null) {
            throw new BusinessException("文章不存在");
        }

        return JsonResult.ok().setData(worldGameArticle);
    }

    @Override
    public List<WorldGameArticle> listAll() {
        return baseMapper.selectList(null);
    }

}
